package com.njupt.Tree;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @Author: wujiaming
 * @CreateTime: 2024/12/8 17:25
 * @Description: 450. 删除二叉搜索树中的节点
 * @Version: 1.0
 */


public class DeleteNode {

    /**
     * 非递归算法
     * @param root
     * @param key
     * @return
     */
    public TreeNode deleteNode(TreeNode root, int key) {
        if(root == null){
            return null;
        }
        if(root.val == key){
            if(root.left == null){
                return root.right;
            } else if (root.right == null) {
                return root.left;
            }else {
                TreeNode treeNode = root.right;
                while(treeNode.left != null){
                    treeNode = treeNode.left;
                }
                treeNode.left = root.left;
                root = root.right;
                return root;
            }
        }
        if(key<root.val) root.left = deleteNode(root.left,key);
        if(key>root.val) root.right = deleteNode(root.right,key);
        return root;
    }

    public TreeNode deleteNode1(TreeNode root, int key){
        if(root == null){
            return null;
        }
        TreeNode pre = null;
        TreeNode cur = root;
        while(cur != null){
            if(cur.val == key){
                break;
            }
            pre = cur;
            if(key<cur.val){
                cur = cur.left;
            }else {
                cur = cur.right;
            }
        }
        //没有找到节点
        if(cur == null){
            return root;
        }
        if(pre == null){
            return deleteOneNode(root);
        }

        if(pre.left != null && pre.left.val == key){
            pre.left = deleteOneNode(cur);
        }
        if(pre.right != null && pre.right.val == key){
            pre.right = deleteOneNode(cur);
        }
        return root;
    }

    public TreeNode deleteOneNode(TreeNode targetNode){
        if(targetNode == null){
            return null;
        }
        if(targetNode.left == null){
            return targetNode.right;
        } else if (targetNode.right == null) {
            return targetNode.left;
        }else {
            TreeNode cur = targetNode.right;
            while(cur.left != null){
                cur = cur.left;
            }
            cur.left = targetNode.left;
            return targetNode.right;
        }

    }


    public static void main(String[] args) {
//        TreeNode root = new TreeNode(5);
//        root.left = new TreeNode(3);
//        root.right = new TreeNode(6);
//        root.left.left = new TreeNode(2);
//        root.left.right = new TreeNode(4);
//        root.right.right = new TreeNode(7);


//        TreeNode root = new TreeNode(50);
//        root.left = new TreeNode(30);
//        root.right = new TreeNode(70);
//
//        root.left.right = new TreeNode(40);
//        root.right.left = new TreeNode(60);
//        root.right.right = new TreeNode(80);
        TreeNode root = new TreeNode(3);
        root.left = new TreeNode(1);
        root.right = new TreeNode(4);

        root.left.right = new TreeNode(2);


        DeleteNode test = new DeleteNode();
        TreeNode root1 = test.deleteNode(root, 2);
        System.out.println();
//        DeleteNode test = new DeleteNode();
//        TreeNode treeNode = test.deleteNode(root, 3);
//        System.out.println(treeNode.val);
    }
}
